//
//  GotoPhotoBrowser.m
//  stone-story
//
//  Created by liu dodo on 12-9-27.
//  Copyright (c) 2012年 liu dodo. All rights reserved.
//

#import "GotoPhotoBrowser.h"

@interface GotoPhotoBrowser ()

@end

@implementation GotoPhotoBrowser
@synthesize photos = _photos;
@synthesize path = _path;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
   // [self goToPhotoBrowserWithPath:self.path ];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation==UIInterfaceOrientationLandscapeRight  );
}



#pragma mark 相册部分
- (MWPhotoBrowser*)goToPhotoBrowserWithPath:(NSString*)pathString
{
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    MWPhoto *photo;
    NSMutableArray *photosArray=[self getAllPhotosWithPath:pathString];
    for(int i=0;i<photosArray.count;i++)
    {
        //Documents/productPhotos/
        NSString *fullPath=[pathString stringByAppendingString:[photosArray objectAtIndex:i]];
        photo = [MWPhoto photoWithFilePath:[NSHomeDirectory() stringByAppendingPathComponent:fullPath]];
        //photo.caption = @"Grotto of the Madonna";
        [photos addObject:photo];
    }
    self.photos = photos;
	MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = YES;
    //browser.wantsFullScreenLayout = NO;//[browser setInitialPageIndex:2];
   //[self.navigationController pushViewController:browser animated:YES];
    return browser;
   
}

- (NSMutableArray*)getAllPhotosWithPath:(NSString*)pathString
{
    
    NSString *filePath =[NSHomeDirectory() stringByAppendingPathComponent:pathString];
    NSError *error = nil;
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:&error];
    NSMutableArray *fileNames=[[NSMutableArray alloc]init];
    NSLog(@"%@", dirContents);
    if ( ! error) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH[cd] '.jpg'"];
        NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate];
        
        fileNames = [NSMutableArray arrayWithArray:imagesOnly];
    }
    //NSLog(@"photos count is=%d",[fileNames count]);
    // count = [fileNames count];  //得到数量多少
    return fileNames;
}
#pragma mark - 相册部分 委托

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    NSLog(@"%d",_photos.count);
    return _photos.count;
}

- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}



@end
